package com.hxxy.wyp.petsLover.common.services.base.backend.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.hxxy.wyp.petsLover.common.entities.HomeInfo;
import com.hxxy.wyp.petsLover.common.entities.News;
import com.hxxy.wyp.petsLover.common.entities.vo.req.HomeInfoReq;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.HomeInfoVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.Select;
import com.hxxy.wyp.petsLover.common.mapper.HomeInfoMapper;
import com.hxxy.wyp.petsLover.common.services.base.backend.HomeInfoService;
import com.hxxy.wyp.petsLover.common.services.base.backend.NewsService;
import com.hxxy.wyp.petsLover.common.services.base.fronted.impl.UserServiceImpl;
import com.hxxy.wyp.petsLover.config.RedisConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ClassName HomeInfoServiceImpl
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/11 14:10
 * @Version 1.0
 */
@Service
public class HomeInfoServiceImpl extends ServiceImpl<HomeInfoMapper, HomeInfo> implements HomeInfoService {
    @Resource
    private HomeInfoMapper homeInfoMapper;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private NewsService newsService;

    @Override
    public HomeInfoVo findAllInfo() {
        HomeInfoVo homeInfoVo = new HomeInfoVo();
        Gson gson = new Gson();
        String info = (String) redisConfig.getRedisTemplateByDb(0).opsForValue().get("homeInfo");
        if (StringUtils.isNoneBlank(info)) {
            return gson.fromJson(info, HomeInfoVo.class);
        }
        List<Select> selects = new ArrayList<>();
        HomeInfo homeInfo = homeInfoMapper.selectById(1L);
        List<String> collect = Arrays.stream(homeInfo.getPic().split(",")).collect(Collectors.toList());
        List<News> list = newsService.list();
        list.forEach(
                news -> {
                    Select select = new Select();
                    List<String> collect1 = Arrays.stream(news.getNewsPic().split(",")).collect(Collectors.toList());
                    news.setNewsPicVo(collect1);
                    select.setValue(news.getId());
                    select.setLabel("新闻ID-" + news.getId());
                    selects.add(select);
                }
        );
        homeInfoVo.setRotographs(collect);
        homeInfoVo.setNewsList(list);
        homeInfoVo.setSelects(selects);
        String s = gson.toJson(homeInfoVo);
        redisConfig.getRedisTemplateByDb(0).opsForValue().set("homeInfo", s);
        redisConfig.getRedisTemplateByDb(0).expire("homeInfo", 1800, TimeUnit.SECONDS);
//        result.getRecords().stream().map(n->n.).collect(Collectors.toList())
        return homeInfoVo;
    }

    @Override
    public HomeInfoVo updateBy(List<HomeInfoReq> fileList) {
        Gson gson = new Gson();
        HomeInfo homeInfo = new HomeInfo();
        String pic = "";
        List<String> listStore = new ArrayList<>();
        for (HomeInfoReq homeInfoReq : fileList) {
            pic = pic + homeInfoReq.getUrl() + ",";
            listStore.add(homeInfoReq.getUrl());
        }
        //有缓存
        String homeInfo1 = (String) redisConfig.getRedisTemplateByDb(0).opsForValue().get("homeInfo");
        if (StringUtils.isNotEmpty(homeInfo1)) {
            HomeInfoVo homeInfoVo = gson.fromJson(homeInfo1, HomeInfoVo.class);
            homeInfoVo.setRotographs(listStore);
            String jsonString = gson.toJson(homeInfoVo);
            redisConfig.getRedisTemplateByDb(0).opsForValue().set("homeInfo", jsonString);
            redisConfig.getRedisTemplateByDb(0).expire("homeInfo", 1800, TimeUnit.SECONDS);
            //更新到数据库
            homeInfo.setId(1L);
            homeInfo.setPic(pic);
            homeInfo.setLastUpdate(LocalDate.now());
            homeInfoMapper.updateById(homeInfo);
            return homeInfoVo;
        }
        //无缓存
        HomeInfoVo allInfo = findAllInfo();
        allInfo.setRotographs(listStore);
        String jsonString = gson.toJson(allInfo);
        redisConfig.getRedisTemplateByDb(0).opsForValue().set("homeInfo", jsonString);
        redisConfig.getRedisTemplateByDb(0).expire("homeInfo", 1800, TimeUnit.SECONDS);
        //更新到数据库
        homeInfo.setId(1L);
        homeInfo.setPic(pic);
        homeInfo.setLastUpdate(LocalDate.now());
        homeInfoMapper.updateById(homeInfo);
        return allInfo;
    }
}
